package com.nosnow.gmall.manage.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nosnow.gmall.bean.SpuImage;
import com.nosnow.gmall.bean.SpuInfo;
import com.nosnow.gmall.bean.SpuSaleAttr;
import com.nosnow.gmall.bean.SpuSaleAttrValue;
import com.nosnow.gmall.service.ManageService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名称：SpuManageController
 * @类描述：
 * @author: bingHeng
 * @Date
 * @Version: 1.0
*/

@Controller
public class SpuManageController {

    @Reference
    private ManageService manageService;

    /**
     * 加载Spu页面
     * @return
     */
    @RequestMapping("spuListPage")
    public String getSpuListPage() {
        return "spuListPage";
    }

    @RequestMapping("spuList")
    @ResponseBody
    public List<SpuInfo> getSpuInfoList(@RequestParam Map<String, String> map) {
        SpuInfo spuInfo = new SpuInfo();
        spuInfo.setCatalog3Id(Long.valueOf(map.get("catalog3Id")));
        List<SpuInfo> spuInfoList = manageService.getSpuInfoList(spuInfo);
        return spuInfoList;
    }

    /**
     * 保存Spu数据
     * @param spuInfo
     * @return
     */
    @RequestMapping("saveSpuInfo")
    @ResponseBody
    public ResponseEntity<Void> saveSpuInfo(SpuInfo spuInfo) {
        manageService.saveSpuInfo(spuInfo);
        return ResponseEntity.ok().build();
    }

    /**
     * 加载化平台属性
     * @param spuId
     * @return
     */
    @RequestMapping("spuImageList")
    @ResponseBody
    public List<SpuImage> getSpuImageList(@RequestParam("spuId") String spuId) {
        return manageService.getSpuImageList(spuId);
    }


    /**
     * 加载销售属性
     * @param httpServletRequest
     * @return
     */
    @RequestMapping("spuSaleAttrList")
    @ResponseBody
    public List<SpuSaleAttr> getSpuSaleAttrList (HttpServletRequest httpServletRequest) {
        String spuId = httpServletRequest.getParameter("spuId");
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(spuId);
        for (SpuSaleAttr spuSaleAttr : spuSaleAttrList) {
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
            Map<String, Object> map = new HashMap<>(16);
            map.put("total",spuSaleAttrValueList.size());
            map.put("rows",spuSaleAttrValueList);
            spuSaleAttr.setSpuSaleAttrValueJson(map);
        }
        return spuSaleAttrList;
    }
}
